package com.atguigu.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.order.bean.Order;
import com.atguigu.order.feign.OrderFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    OrderFeignClient orderFeignClient;

    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId, Long userId) {
        Order order = new Order();
        order.setId(1L);
        Product product = getProductForRemoteWithFeign(productId);
        //  总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("shangguigu");
        //  远程查询
        order.setProductList(List.of(product));


        return order;
    }
    
    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(1L);
        //  总金额
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息:"+e.getClass());
        //  远程查询
        
        
        return order;
    }

    // 4 openfeign负载均衡请求
    public Product getProductForRemoteWithFeign(Long productId) {
        return orderFeignClient.getProduct(productId);
    }

    // 3 负载均衡请求
    public Product getProductForRemoteWithLoadBalanceAnnotation(Long productId) {
        // 获取商品服务的所有机器IP+端口
        // 拼接url  service-product 会被动态替换成微服务IP+端口
        String url = "http://service-product/product/"+productId;
        log.info("远程请求:{}", url);
        // 给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    // 2 负载均衡请求
    public Product getProductForRemoteWithLoadBalance(Long productId) {
        // 获取商品服务的所有机器IP+端口
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        // 拼接url
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + productId;
        log.info("远程请求:{}", url);
        // 给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    public Product getProductForRemote(Long productId) {
        // 获取商品服务的所有机器IP+端口
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        // 拼接url
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求:{}", url);
        // 给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
